void setup() {
  // put your setup code here, to run once:

}

void loop() {
  if (localClient && localClient.connected() ) {
    if (localClient.available()) {
      size_t len = localClient.available();
      uint8_t sbuf[len];
      localClient.readBytes(sbuf, len);
      wyslij_na_RS485(sbuf, len);
    }
  }
  
  void wyslij_na_RS485( uint8_t *sbuf, size_t len) {
    digitalWrite(DIR485, HIGH);
    Serial.write(sbuf, len);
    // Get the number of bytes (characters) available for writing 
    //in the serial buffer without blocking the write operation.
    while ( Serial.availableForWrite() != lenBufSerTx ) ; 
    // Konieczne, bo używając "Serial.availableForWrite()" 
    //stwierdzimy kiedy bufor jest pusty a nie kiedy znak wyslano z UART
    delayMicroseconds( TIM_SEND_BYTE_UART ); 
    digitalWrite(DIR485, LOW);
  }
}